package com.yugu.park.business.service.impl;



import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugu.park.business.entity.AdmissionRecord;
import com.yugu.park.business.entity.CardInfo;
import com.yugu.park.business.entity.MemberInfo;
import com.yugu.park.business.mapper.AdmissionRecordMapper;
import com.yugu.park.business.mapper.CardInfoMapper;
import com.yugu.park.business.mapper.MemberInfoMapper;
import com.yugu.park.business.service.AdmissionRecordService;
import com.yugu.park.dto.AdmissionRecordDto;
import com.yugu.park.enums.SequenceTypeEnum;
import com.yugu.park.enums.SystemEnum;
import com.yugu.park.exception.BusinessException;
import com.yugu.util.BeanCopyUtil;
import com.yugu.util.DataUtils;
import com.yugu.util.SearchPage;
import com.yugu.util.SysQueryParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Noble-Six
* @description 针对表【t_admission_record】的数据库操作Service实现
* @createDate 2024-04-25 10:59:55
*/
@Service
public class AdmissionRecordServiceImpl extends ServiceImpl<AdmissionRecordMapper, AdmissionRecord>
    implements AdmissionRecordService {

    @Resource
    private AdmissionRecordMapper admissionRecordMapper;

    @Resource
    private CardInfoMapper cardInfoMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdate(AdmissionRecord record) {
        AdmissionRecord old = admissionRecordMapper.selectById(record.getId());
        if (old != null) {
            BeanCopyUtil.copyProperties(record,old);
            admissionRecordMapper.updateById(old);
        }else {
            //放入redis是否入园
            String key = SystemEnum.ADMINSSION_RECORD_.getCode();
            Boolean member = redisTemplate.opsForSet().isMember(key, record.getCardId());
            if (member != null && member) {
                throw new BusinessException("已进入游乐场");
            }
            redisTemplate.opsForSet().add(key,record.getCardId());
            record.setId(IdUtil.simpleUUID());
            CardInfo cardInfo = cardInfoMapper.selectById(record.getCardId());
            if(cardInfo.getStatus()!=1){
                throw new BusinessException("会员到期或作废");
            }
            cardInfo.setCount(cardInfo.getCount() + 1);
            cardInfoMapper.updateById(cardInfo);
            record.setInTime(new Date());
            admissionRecordMapper.insert(record);
        }
    }

    @Override
    public PageInfo<AdmissionRecordDto> page(SearchPage<SysQueryParam> search) {
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<AdmissionRecordDto> list = admissionRecordMapper.page(search.getSearch());
        return PageInfo.of(list);
    }
}




